package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public int delete(Long id) {
        return departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int save(Department department) {
        return departmentMapper.insert(department);
    }

    @Override
    public Department get(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public int update(Department department) {
        return departmentMapper.updateByPrimaryKey(department);
    }

    @Override
    public PageInfo<Department> query(QueryObject qo) {

        //int count = departmentMapper.selectForCount(qo);
        //if(count == 0) {
        //    return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), Collections.emptyList(), count);
        //}
        //
        //// 获取总页数
        //int totalPage =  count % qo.getPageSize() == 0?
        //        count / qo.getPageSize() : count / qo.getPageSize() + 1;
        //// 判断当前页是否大于总页数, 如果是, 把总页数作为当前页来查询
        //qo.setCurrentPage(totalPage <= qo.getCurrentPage() ? totalPage : qo.getCurrentPage());
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Department> departments = departmentMapper.selectForList(qo);
        return new PageInfo<>(departments);
    }
}
